package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> seletePage(int page, int pageSize) {
        log.info("页数:{},一页{}条数据.", page, pageSize);
        Page<Category> categoryPage = categoryService.seletePage(page, pageSize);
        return R.success(categoryPage);
    }

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return R.success("新增分类成功");
    }

    /**
     * 修改菜品
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> editCategory(@RequestBody Category category) {
        categoryService.editCategory(category);
        return R.success("修改分类成功");
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory(Long id) {
        categoryService.deleteCategory(id);
        return R.success("分类删除成功");
    }

    /**
     * 根据分类类型,查询分类数据
     *
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Category category) {
        List<Category> categoryList = categoryService.getCategoryList(category);
        return R.success(categoryList);
    }
}
